package com.itheima;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * mybatis的代理开发
 */
public class myBatisDemo2 {
    public static void main(String[] args) throws IOException {
        //1.加载mybatis的核心配置文件,获取sqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //获取sqlSession对象,执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行sql
        //List<Object> objects = sqlSession.selectList("text.selectAll");
        //获取UserMapper接口的代理对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.selectAll();
        for (Object object : users) {
            System.out.println(object);
        }
        selectAll();
        //释放资源
        sqlSession.close();
    }

    public static void selectAll() throws IOException {
        //1.加载mybatis的核心配置文件,获取sqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //获取sqlSession对象,执行sql
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行sql
        //List<Object> objects = sqlSession.selectList("text.selectAll");
        //获取UserMapper接口的代理对象
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = mapper.selectAll1(1);
        for (Object object : users) {
            System.out.println(object);
        }
        //释放资源
        sqlSession.close();
    }
}
